
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dvd.gestionnaires;

import dvd.modeles.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class gestionnaireCommande {

    @PersistenceContext
    private EntityManager em;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")


    public Commande creeCommande(Utilisateur u, Panier panier, String nom, String prenom, String adresse, String codepostal, String ville) {
       
        Commande c = new Commande (u,panier, nom, prenom, adresse, codepostal, ville);
        em.persist(c);
         for(int i=0;i<panier.getLignePanier().size(); i++)
        {
            c.addLigneCommande(panier.getLignePanier().get(i));
            Film f = this.getFilmFromId(panier.getLignePanier().get(i).getFilm().getId());
            f.setStock((f.getStock()-panier.getLignePanier().get(i).getQuantite()));
            em.merge(f);
        }  
        
        em.persist(c); 
        return c;
    }   
    
        public Film getFilmFromId(final int filmid) {
        return em.find(Film.class, filmid);
    }
}
